package com.example.springbootdemo.util;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

public class KeyLock {

    private static final Map<String, AtomicBoolean> lockMap = new ConcurrentHashMap<>();


    public static boolean lock(String key) {
        AtomicBoolean atomicBoolean = lockMap.computeIfAbsent(key, k -> new AtomicBoolean(false));
        return atomicBoolean.compareAndSet(false, true);
    }

    public static void unlock(String key) {
        lockMap.remove(key);
    }
}
